//
//  File.swift
//
//
//  Created by WeCloudIOS on 2022/4/13.
//

import Foundation
import UIKit
import CoreGraphics
import AVFAudio
import SwiftUI

class ControlView : UIView{
    
    var isShow = true
    var listen: RoomListener?
    var observable:WCObservableRoom?
    var mutes = false
    var cameras = false
    var louds = false
    init() {
        self.listen = nil
        super.init(frame: CGRect.zero)
    }
    
    public convenience init(cv: RoomListener,camera: Bool,mute: Bool,loud: Bool){
        self.init()
        mutes = mute
        cameras = camera
        louds = loud
        setUI()
        setPlayType(type:!loud)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        backgroundColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.7)
        layer.cornerRadius = 20
        layer.masksToBounds = true
        addSubview(effectView)
        addSubview(downBtn)
        addSubview(btnMute)
        addSubview(btnLoudspeaker)
        addSubview(btnEndCall)
        addSubview(btnCamera)
        addSubview(btnAddUser)
    }
    
    
    @objc func layoutChange() {
        isShow = !isShow
        if isShow{//展示
            btnMute.isHidden = false
            btnLoudspeaker.isHidden = false
            btnEndCall.isHidden = false
            btnCamera.isHidden = false
            btnAddUser.isHidden = false
            let image = Tool.SCImage(named: "show_on")
            downBtn.setImage(image, for: .normal)
            UIView.animate(withDuration: 0.3) {
                let space = (Int(Windos.w) - 20)/5+45+40
                self.frame = CGRect(x: 10, y: Int(Windos.h)-space-20, width: Int(Windos.w)-20, height: space)
                self.listen?.upDateUI()
            }
        }else{//隐藏
            UIView.animate(withDuration: 0.3) {
                self.btnMute.isHidden = true
                self.btnLoudspeaker.isHidden = true
                self.btnEndCall.isHidden = true
                self.btnCamera.isHidden = true
                self.btnAddUser.isHidden = true
                let image = Tool.SCImage(named: "show_off")
                self.downBtn.setImage(image, for: .normal)
                self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y+self.frame.size.height-40, width: self.frame.size.width, height: 40)
                self.listen?.upDateUI()
            }
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let space = (Int(Windos.w) - 20)/5
        effectView.frame = CGRect(x: 0, y: 0, width: Windos.w-20, height: frame.size.height)
        btnMute.frame = CGRect(x: 0, y: 40, width: space, height: space+25)
        btnLoudspeaker.frame = CGRect(x: space, y: 40, width: space, height: space+25)
        btnEndCall.frame = CGRect(x: space*2, y: 40, width: space, height: space+25)
        btnCamera.frame = CGRect(x: space*3, y: 40, width: space, height: space+25)
        btnAddUser.frame = CGRect(x: space*4, y: 40, width: space, height: space+25)
        downBtn.frame = CGRect(x: 0, y: 0, width: Windos.w-20, height: 40)
        
    }
    
    lazy var effectView: UIVisualEffectView = {
        let blur = UIBlurEffect.init(style:.dark)
        let effect = UIVisualEffectView(effect: blur)
        return effect
    }()
    
    lazy var btnMute: LayoutButton = {
        let btn = LayoutButton()
        btn.stateLabel.text = "静音"
        btn.stateLabel.font = UIFont.systemFont(ofSize: 13)
        btn.btn.tag = 1000;
        let image = Tool.SCImage(named: "speak_closed")
        let selectImage = Tool.SCImage(named: "video_close_voice")
        btn.btn.setImage(selectImage, for: .selected)
        btn.btn.setImage(image, for: .normal)
        btn.btn.addTarget(self, action: #selector(tapAction(button:)), for: .touchUpInside)
        
        if mutes{
            btn.btn.isSelected = true
        }
        
        return btn
    }()
    
    lazy var btnLoudspeaker: LayoutButton = {
        let btn = LayoutButton()
        btn.stateLabel.text = "扬声器"
        btn.stateLabel.font = UIFont.systemFont(ofSize: 13)
        btn.btn.tag = 1001;
        let image = Tool.SCImage(named: "voice_open")
        let selectImage = Tool.SCImage(named: "video_voice")
        btn.btn.setImage(selectImage, for: .selected)
        btn.btn.setImage(image, for: .normal)
        btn.btn.addTarget(self, action: #selector(tapAction(button:)), for: .touchUpInside)
        
        if !louds{
            btn.btn.isSelected = true
        }
        return btn
    }()
    
    lazy var btnEndCall: LayoutButton = {
        let btn = LayoutButton()
        btn.stateLabel.text = "挂断"
        btn.stateLabel.font = UIFont.systemFont(ofSize: 13)
        btn.btn.tag = 1002;
        let image = Tool.SCImage(named: "video_hang_down")
        btn.btn.setImage(image, for: .normal)
        btn.btn.addTarget(self, action: #selector(tapAction(button:)), for: .touchUpInside)
        return btn
    }()
    
    lazy var btnCamera: LayoutButton = {
        let btn = LayoutButton()
        btn.stateLabel.text = "摄像头"
        btn.stateLabel.font = UIFont.systemFont(ofSize: 13)
        btn.btn.tag = 1003;
        let image = Tool.SCImage(named: "video_camera")
        let selectImage = Tool.SCImage(named: "video_camera_close")
        btn.btn.setImage(selectImage, for: .selected)
        btn.btn.setImage(image, for: .normal)
        btn.btn.addTarget(self, action: #selector(tapAction(button:)), for: .touchUpInside)
        
        if !cameras{
            btn.btn.isSelected = true
        }
        return btn
    }()
    
    lazy var btnAddUser: LayoutButton = {
        let btn = LayoutButton()
        btn.stateLabel.text = "添加成员"
        btn.stateLabel.font = UIFont.systemFont(ofSize: 13)
        btn.btn.tag = 1004;
        let image = Tool.SCImage(named: "add_user")
        btn.btn.setImage(image, for: .normal)
        btn.btn.addTarget(self, action: #selector(tapAction(button:)), for: .touchUpInside)
        return btn
    }()
    
    lazy var downBtn: UIButton = {
        let btn = UIButton()
        let image = Tool.SCImage(named: "show_on")
        btn.setImage(image, for: .normal)
        btn.addTarget(self, action: #selector(layoutChange), for: .touchUpInside)
        return btn
    }()
    
    @objc func tapAction(button: UIButton)->Void{
        
        switch button.tag {
        case 1000:
            button.isSelected = !button.isSelected
            mutes = !mutes
//            observable?.room.localParticipant?.setMicrophone(enabled: mutes)
            observable?.toggleMicrophoneEnabled()
        case 1001:
            button.isSelected = !button.isSelected
            louds = !louds
            setPlayType(type: !louds)
            
        case 1002:
            observable?.room.disconnect()
            listen?.back()
        case 1003:
            button.isSelected = !button.isSelected
            cameras = !cameras
            listen?.closeCamera(close: cameras)
            observable?.toggleCameraEnabled()
        default:
            listen?.addUser()
        }
    }
    
    func setPlayType(type: Bool) {
        if type{//听筒
            let session = AVAudioSession()
            do{
                try session.setCategory(.playAndRecord,mode: .voiceChat)
                try session.setActive(true)
            }catch{}
        }else{//扬声器模式
            let session = AVAudioSession()
            do{
                try session.setCategory(.playback,mode: .voiceChat)
                try session.setActive(true)
            }catch{}
        }
    }
}



